#include <stdlib.h>
#include <stdio.h>

typedef struct treenode *Treeptr;
typedef struct treenode{
  int id;
  Treeptr parent;
  Treeptr left;
  Treeptr right;
} Treenode;

Treeptr root;


Treeptr create_node(int id)
{
  Treeptr p = (Treeptr)malloc(sizeof(Treenode));
  p->id = id;
  p->parent = p->left = p->right =NULL;
  return p;
}

void link_node(Treeptr parent, Treeptr left, Treeptr right)
{
  parent->left = left;
  parent->right = right;
  if(left != NULL)
    left->parent = parent;
  if(right != NULL)
    right->parent = parent;
  return;
}

void make_tree()
{
  Treeptr t2,t3,t4,t5,t6,t7,t9,t11;
  root = create_node(1);
  t2 = create_node(2);
  t3 = create_node(3);
  t4 = create_node(4);
  t5 = create_node(5);
  t6 = create_node(6);
  t7 = create_node(7);
  t9 = create_node(9);
  t11 = create_node(11);

  
  link_node(root,t7,t3);

  link_node(t7,t2,t6);
  link_node(t6,t5,t11);
  link_node(t3,NULL,t9);
  link_node(t9,t4,NULL);

}

int get_depth(Treeptr node,int depth)
{
  static int maxdepth = 1;
  if(node == NULL)
    return 0;
  else{
    printf("id: %d\n",node->id);
    if(depth>maxdepth)
      maxdepth = depth;
    get_depth(node->left,depth+1);
    get_depth(node->right,depth+1);
  }
  return maxdepth;
}
int get_depth_bottom_up(Treeptr node)
{
  int l_depth,r_depth;
  if(node == NULL)
    return 0;
  else {
    l_depth = get_depth_bottom_up(node->left)+1;
    r_depth = get_depth_bottom_up(node->right)+1;
    return l_depth>r_depth ? l_depth : r_depth;
  }
}

int main()
{
  int i;
  make_tree();
  i = get_depth_bottom_up(root);
  printf("%d\n",i);
  return 0;
}
